﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Data.Exceller;
using H2.NET.Business;

namespace H2.NET.Web.Pages
{
    public partial class TabInsPlan : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        private string InsPlan_1_Material_Code
        {
            get
            {
                return ViewState["InsPlan_1_Material_Code"] == null ? "" : (string)ViewState["InsPlan_1_Material_Code"];

            }
            set { ViewState["InsPlan_1_Material_Code"] = value; }
        }

        //private string Insplan_2.MstrChar_Code
        //{
        //    get
        //    {
        //        return ViewState["Insplan_2.MstrChar_Code"] == null ? "" : (string)ViewState["Insplan_2.MstrChar_Code"];

        //    }
        //    set { ViewState["Insplan_2.MstrChar_Code"] = value; }
        //}

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

        public List<object> InsPlan_1
        {
            get
            {
                if (StoreAPI["InsPlan_1~" + Key] == null)
                {
                    StoreAPI["InsPlan_1~" + Key] = new List<object>();
                }

                return (List<object>)StoreAPI["InsPlan_1~" + Key];
            }
            set { StoreAPI["InsPlan_1~" + Key] = value; }
        }

        public List<object> InsPlan_2
        {
            get
            {
                if (StoreAPI["InsPlan_2~" + Key] == null)
                {
                    StoreAPI["InsPlan_2~" + Key] = new List<object>();
                }

                return (List<object>)StoreAPI["InsPlan_2~" + Key];
            }
            set { StoreAPI["InsPlan_2~" + Key] = value; }
        }

        private string[] P_Plant
        {
            get
            {
                if (StoreAPI["P_Plant~" + Key] == null)
                {
                    IList<m_Plant> dlts = (m_Plant.status == 1 & m_Plant.categary == "P").List<m_Plant>();
                    string[] p_plant = new string[dlts.Count+1];

                    p_plant[0] = "";
                    for (int i = 0; i < dlts.Count; i++)
                    {
                        p_plant[i+1] = dlts[i].PlantName;
                    }

                    StoreAPI["P_Plant~" + Key] = p_plant;
                }

                return (string[])StoreAPI["P_Plant~" + Key];
            }
        }

        #endregion

        #region 事件 Load(页面)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            InitDetails();

            if (!Page.IsPostBack)
            {
                InitControls();
            }
        }
        #endregion

        #region 事件 Sheets 选择
        protected void Sheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SheetIndex != -1)
            {
                InsPlan_1[SheetIndex] = gv_InsPlan_1.DataSource;
                InsPlan_2[SheetIndex] = gv_InsPlan_2.DataSource;
            }

            if (Sheets.SelectedIndex != -1)
            {
                gv_InsPlan_1.DataSource = InsPlan_1[Sheets.SelectedIndex];
                gv_InsPlan_2.DataSource = InsPlan_2[Sheets.SelectedIndex];
                SheetIndex = Sheets.SelectedIndex;
                Anthem.Manager.AddScriptForClientSideEval("CellClick('" + gv_InsPlan_1.ClientID + "','-1','-1');");
            }
        }
        #endregion

        #region 事件 Sheets 增加
        protected void Add_Click(object sender, ImageClickEventArgs e)
        {
            Sheets.Items.Add(new ListItem((Sheets.Items.Count + 1).ToString(), (Sheets.Items.Count + 1).ToString()));
            InsPlan_1[SheetIndex] = gv_InsPlan_1.DataSource;
            InsPlan_2[SheetIndex] = gv_InsPlan_2.DataSource;

            List<mdc_Ins_Plan_1> insp_1_s = new List<mdc_Ins_Plan_1>();
            for (int i = 0; i < 2; i++)
            {
                insp_1_s.Add(new mdc_Ins_Plan_1());
                insp_1_s[i].No = i + 1;
                insp_1_s[i].Status = 1;
            }
            InsPlan_1.Add(insp_1_s);

            List<mdc_Ins_Plan_2> insp_2_s = new List<mdc_Ins_Plan_2>();
            for (int i = 0; i < 9; i++)
            {
                insp_2_s.Add(new mdc_Ins_Plan_2());
                insp_2_s[i].No = (i + 1) * 10;
                insp_2_s[i].Status = 1;
            }
            InsPlan_2.Add(insp_2_s);

            SheetsCount.Text = Sheets.Items.Count.ToString();
        }
        #endregion

        #region 事件 Sheets 删除
        protected void Delete_Click(object sender, ImageClickEventArgs e)
        {
            if (Sheets.SelectedIndex != -1)
            {
                //至少保留 1 个 Sheet
                if (Sheets.Items.Count == 1)
                {
                    Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                    return;
                }

               
                InsPlan_1.RemoveAt(Sheets.SelectedIndex);
                InsPlan_2.RemoveAt(Sheets.SelectedIndex);

                Sheets.Items.RemoveAt(Sheets.SelectedIndex);

                gv_InsPlan_1.DataSource = InsPlan_1[0];
                gv_InsPlan_2.DataSource = InsPlan_2[0];

                SheetsCount.Text = Sheets.Items.Count.ToString();

                Sheets.Items.Clear();
                for (int i = 0; i < InsPlan_1.Count; i++)
                {
                    Sheets.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
                    Sheets.SelectedIndex = 0;
                }

                SheetIndex = Sheets.SelectedIndex;
                //Anthem.Manager.AddScriptForClientSideEval("CellClick('" + gv_InsPlan_1.ClientID + "','0','0');");
            }
        }
        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "InsPlan_1_Add":
                    List<mdc_Ins_Plan_1> insp_1_s = (List<mdc_Ins_Plan_1>)gv_InsPlan_1.DataSource;
                    insp_1_s.Insert(gv_InsPlan_1.ChangedRowIndex == -1 || gv_InsPlan_1.ChangedRowIndex >= insp_1_s.Count ? insp_1_s.Count : gv_InsPlan_1.ChangedRowIndex + 1, new mdc_Ins_Plan_1());
                    gv_InsPlan_1.DataSource = insp_1_s;
                    gv_InsPlan_1.DataBind();
                    break;
                case "InsPlan_2_Add":
                    List<mdc_Ins_Plan_2> insp_2_s = (List<mdc_Ins_Plan_2>)gv_InsPlan_2.DataSource;
                    insp_2_s.Insert(gv_InsPlan_2.ChangedRowIndex == -1 || gv_InsPlan_2.ChangedRowIndex >= insp_2_s.Count ? insp_2_s.Count : gv_InsPlan_2.ChangedRowIndex + 1, new mdc_Ins_Plan_2());
                    for (int i = 0; i < insp_2_s.Count; i++)
                    {
                        insp_2_s[i].No = (i + 1) * 10;
                    }
                    gv_InsPlan_2.DataSource = insp_2_s;
                    gv_InsPlan_2.DataBind();
                    break;
            }
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "InsPlan_1_Delete":
                    List<mdc_Ins_Plan_1> insp_1_s = (List<mdc_Ins_Plan_1>)gv_InsPlan_1.DataSource;
                    if (insp_1_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一行 ！',false)");
                        return;
                    }
                    insp_1_s.RemoveAt(gv_InsPlan_1.ChangedRowIndex == -1 || gv_InsPlan_1.ChangedRowIndex >= insp_1_s.Count ? insp_1_s.Count - 1 : gv_InsPlan_1.ChangedRowIndex);
                    gv_InsPlan_1.DataSource = insp_1_s;
                    gv_InsPlan_1.DataBind();
                    break;
                case "InsPlan_2_Delete":
                    List<mdc_Ins_Plan_2> insp_2_s = (List<mdc_Ins_Plan_2>)gv_InsPlan_2.DataSource;
                    if (insp_2_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一行 ！',false)");
                        return;
                    }
                    insp_2_s.RemoveAt(gv_InsPlan_2.ChangedRowIndex == -1 || gv_InsPlan_2.ChangedRowIndex >= insp_2_s.Count ? insp_2_s.Count - 1 : gv_InsPlan_2.ChangedRowIndex);
                    for (int i = 0; i < insp_2_s.Count; i++)
                    {
                        insp_2_s[i].No = (i + 1) * 10;
                    }
                    gv_InsPlan_2.DataSource = insp_2_s;
                    gv_InsPlan_2.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            if (SheetIndex != -1)
            {
                InsPlan_1[SheetIndex] = gv_InsPlan_1.DataSource;
                InsPlan_2[SheetIndex] = gv_InsPlan_2.DataSource;
            }

            //将数据库里当前流程对应的 mdc_Ins_Plan_1 状态置为逻辑删除
            (mdc_Ins_Plan_1.process == TaskAPI.Process & mdc_Ins_Plan_1.incident == TaskAPI.Incident).Edit<mdc_Ins_Plan_1>(o =>
            {
                o.Status = 0;
            }
            );

            //将数据库里当前流程对应的 mdc_Ins_Plan_2 状态置为逻辑删除
            (mdc_Ins_Plan_2.process == TaskAPI.Process & mdc_Ins_Plan_2.incident == TaskAPI.Incident).Edit<mdc_Ins_Plan_2>(o =>
            {
                o.Status = 0;
            }
            );

            //更新最新数据状态(包括 Insert、Update)
            for (int i = 0; i < InsPlan_1.Count; i++)
            {
                bool flg = true;
                foreach (mdc_Ins_Plan_1 insp_1 in (List<mdc_Ins_Plan_1>)InsPlan_1[i])
                {
                    if (string.IsNullOrEmpty(insp_1.Material_Code))
                    {
                        continue;
                    }

                    insp_1.Process  = TaskAPI.Process;
                    insp_1.Incident = TaskAPI.Incident;
                    insp_1.UserName = UserAPI.UserName;
                    insp_1.Sheets   = i;
                    insp_1.Time     = System.DateTime.Now;
                    insp_1.Status   = 1;

                    flg = false;
                    Storage.Save(insp_1);
                }

                if (flg)
                {
                    mdc_Ins_Plan_1 insp_1 = new mdc_Ins_Plan_1();
                    insp_1.Process  = TaskAPI.Process;
                    insp_1.Incident = TaskAPI.Incident;
                    insp_1.UserName = UserAPI.UserName;
                    insp_1.Sheets   = i;
                    insp_1.Time     = System.DateTime.Now;
                    insp_1.Status   = 1;

                    Storage.Save(insp_1);
                }
            }

            //更新最新数据状态(包括 Insert、Update)
            for (int i = 0; i < InsPlan_2.Count; i++)
            {
                bool flg = true;
                foreach (mdc_Ins_Plan_2 insp_2 in (List<mdc_Ins_Plan_2>)InsPlan_2[i])
                {
                    if (string.IsNullOrEmpty(insp_2.MstrChar_Code))
                    {
                        continue;
                    }

                    insp_2.Process  = TaskAPI.Process;
                    insp_2.Incident = TaskAPI.Incident;
                    insp_2.UserName = UserAPI.UserName;
                    insp_2.Sheets   = i;
                    insp_2.Time     = System.DateTime.Now;
                    insp_2.Status   = 1;

                    flg = false;
                    Storage.Save(insp_2);
                }

                if (flg)
                {
                    mdc_Ins_Plan_2 insp_2 = new mdc_Ins_Plan_2();
                    insp_2.Process  = TaskAPI.Process;
                    insp_2.Incident = TaskAPI.Incident;
                    insp_2.UserName = UserAPI.UserName;
                    insp_2.Sheets   = i;
                    insp_2.Time     = System.DateTime.Now;
                    insp_2.Status   = 1;

                    Storage.Save(insp_2);
                }
            }
            TaskAPI["Material_Code"] = InsPlan_1_Material_Code;
        }
        #endregion

        #region 方法 InitControls(初始化)
        public void InitControls()
        {
            gv_InsPlan_1.Key = Key;
            gv_InsPlan_2.Key = Key;

            #region 发起步骤
            if (TaskAPI.IsInitiate)
            {
                Sheets.Items.Add(new ListItem("1", "1"));
                SheetsCount.Text = Sheets.Items.Count.ToString();

                List<mdc_Ins_Plan_1> insp_1_s = new List<mdc_Ins_Plan_1>();
                for (int i = 0; i < 2; i++)
                {
                    insp_1_s.Add(new mdc_Ins_Plan_1());
                    insp_1_s[i].No = i + 1;
                    insp_1_s[i].Status = 1;
                }
                gv_InsPlan_1.DataSource = insp_1_s;
                InsPlan_1.Add(insp_1_s);

                List<mdc_Ins_Plan_2> insp_2_s = new List<mdc_Ins_Plan_2>();
                for (int i = 0; i < 9; i++)
                {
                    insp_2_s.Add(new mdc_Ins_Plan_2());
                    insp_2_s[i].No = (i + 1) * 10;
                    insp_2_s[i].Status = 1;
                }
                gv_InsPlan_2.DataSource = insp_2_s;
                InsPlan_2.Add(insp_2_s);
            }
            #endregion

            #region 审批环节
            else
            {
                InsPlan_1.Clear();
                InsPlan_2.Clear();

                #region 将数据库里的数据分配到各 Sheets
                IList<mdc_Ins_Plan_1> insp_1_s = (mdc_Ins_Plan_1.process == TaskAPI.Process & mdc_Ins_Plan_1.incident == TaskAPI.Incident & mdc_Ins_Plan_1.status == 1).List<mdc_Ins_Plan_1>(new Region(0, 4000), mdc_Ins_Plan_1.sheets.Asc);
                if (insp_1_s.Count == 0)
                {
                    InsPlan_1.Add(new List<mdc_Ins_Plan_1>());
                    for (int i = 0; i < 2; i++)
                    {
                        insp_1_s.Add(new mdc_Ins_Plan_1());
                        insp_1_s[i].No = i + 1;
                        insp_1_s[i].Status = 1;
                    }
                    InsPlan_1[0] = insp_1_s;
                }
                else
                {
                    foreach (mdc_Ins_Plan_1 insp_1 in insp_1_s)
                    {
                        if (Sheets.Items.FindByValue(insp_1.Sheets.ToString()) == null)
                        {
                            Sheets.Items.Add(new ListItem((insp_1.Sheets + 1).ToString(), insp_1.Sheets.ToString()));
                            InsPlan_1.Add(new List<mdc_Ins_Plan_1>());
                            InsPlan_2.Add(new List<mdc_Ins_Plan_2>());
                        }

                        ((List<mdc_Ins_Plan_1>)InsPlan_1[insp_1.Sheets]).Add(insp_1);
                    }
                }
                gv_InsPlan_1.DataSource = InsPlan_1[0];
                #endregion

                #region 将数据库里的数据分配到各 Sheets
                IList<mdc_Ins_Plan_2> insp_2_s = (mdc_Ins_Plan_2.process == TaskAPI.Process & mdc_Ins_Plan_2.incident == TaskAPI.Incident & mdc_Ins_Plan_2.status == 1).List<mdc_Ins_Plan_2>(new Region(0, 4000), mdc_Ins_Plan_2.sheets.Asc);
                if (insp_2_s.Count == 0)
                {
                    InsPlan_2.Add(new List<mdc_Ins_Plan_2>());
                    for (int i = 0; i < 9; i++)
                    {
                        insp_2_s.Add(new mdc_Ins_Plan_2());
                        insp_2_s[i].No = (i + 1) * 10;
                        insp_2_s[i].Status = 1;
                    }

                    InsPlan_2[0] = insp_2_s;
                }
                else
                {
                    foreach (mdc_Ins_Plan_2 insp_2 in insp_2_s)
                    {
                        ((List<mdc_Ins_Plan_2>)InsPlan_2[insp_2.Sheets]).Add(insp_2);
                    }
                }
                gv_InsPlan_2.DataSource = InsPlan_2[0];
                #endregion

                SheetsCount.Text = Sheets.Items.Count.ToString();
            }
            #endregion

            SetVisible(Status.IsEdited && TaskAPI.IsAllowSubmit, InsPlan_1_Add, InsPlan_1_Delete, InsPlan_2_Add, InsPlan_2_Delete, Add, Delete, Import, Export);
            Attachment1.AllowUpload = Status.IsEdited && TaskAPI.IsAllowSubmit;
            Attachment1.AllowDelete = Status.IsEdited && TaskAPI.IsAllowSubmit;
        }
        #endregion

        #region 方法 InitDetails(初始化明细行)
        public void InitDetails()
        {
            bool flg = Status.IsEdited && TaskAPI.IsAllowSubmit;
            gv_InsPlan_1.DynamicColumn.Add(new BoundColumn2("00", "SAP Code" + (TaskAPI.Process == "New Raw Material Introducing" ? "\n(填写A-Coat)" : ""), BoundColumnType2.TextBox, 104, Align.Left, flg, "Material_Code", InputType.None, null));
            gv_InsPlan_1.DynamicColumn.Add(new BoundColumn2("01", "Plant", BoundColumnType2.DropDownList, 60, Align.Left, flg, "Plant", InputType.None, P_Plant));
            gv_InsPlan_1.DynamicColumn.Add(new BoundColumn2("02", "Material Description", BoundColumnType2.TextBox, -1, Align.Left, flg, "Material_Description", InputType.None, null));
            gv_InsPlan_1.DynamicColumn.Add(new BoundColumn2("03", "IP Group", BoundColumnType2.TextBox, 100, Align.Left, flg, "IP_Group", InputType.None, null));
            gv_InsPlan_1.DynamicColumn.Add(new BoundColumn2("04", "Group Counter", BoundColumnType2.TextBox, 100, Align.Left, flg, "Group_Counter", InputType.None, null));
            gv_InsPlan_1.DynamicColumn.Add(new BoundColumn2("05", "Status", BoundColumnType2.DropDownList, 90, Align.Left, flg, "InsPlan_Status", InputType.None, new string[3] { "", "1, 暂不激活", "4, 直接激活" }));

            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("00", "Item", BoundColumnType2.Label, 34, Align.Left, flg, "No", InputType.None, null));
            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("01", "QN/QL", BoundColumnType2.Label, 70, Align.Left, flg, "QN_QL", InputType.None, null));
            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("02", "MstrChar Plant", BoundColumnType2.Label, 60, Align.Left, flg, "MstrChar_Plant", InputType.None, null));
            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("03", "MstrChar Code", BoundColumnType2.TextBox, 60, Align.Left, flg, "MstrChar_Code", InputType.Numeric, null));
            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("04", "Short text in insp.char", BoundColumnType2.Label, -1, Align.Left, false, "Short_Text_In_Insp_Char", InputType.None, null));
            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("05", "Lower SpecLimit", BoundColumnType2.TextBox, 60, Align.Left, flg, "Lower_SpecLimit", InputType.Numeric, null));
            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("06", "Upper SpecLimit", BoundColumnType2.TextBox, 60, Align.Left, flg, "Upper_SpecLimit", InputType.Numeric, null));
            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("07", "Decimal number", BoundColumnType2.Label, 60, Align.Left, flg, "Decimal_Number", InputType.Numeric, null));
            gv_InsPlan_2.DynamicColumn.Add(new BoundColumn2("08", "Long Text", BoundColumnType2.TextBox, 120, Align.Left, flg, "Long_Text", InputType.None, null));
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!Status.IsEdited)
            {
                return new List<string>();
            }

            if (SheetIndex != -1)
            {
                InsPlan_1[SheetIndex] = gv_InsPlan_1.DataSource;
                InsPlan_2[SheetIndex] = gv_InsPlan_2.DataSource;
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();

            #region InsPlan_1
                for (int i = 0; i < InsPlan_1.Count; i++)
                {
                    List<mdc_Ins_Plan_1> ins_plan_s = InsPlan_1[i] as List<mdc_Ins_Plan_1>;
                    if (ins_plan_s == null)
                    {
                        continue;
                    }

                    foreach (mdc_Ins_Plan_1 ins_plan_1 in ins_plan_s)
                    {
                        if (string.IsNullOrEmpty(ins_plan_1.Material_Code) && string.IsNullOrEmpty(ins_plan_1.Plant) && string.IsNullOrEmpty(ins_plan_1.IP_Group)
                                                                      && string.IsNullOrEmpty(ins_plan_1.Group_Counter) && string.IsNullOrEmpty(ins_plan_1.Material_Description))
                        {
                            continue;
                        }
                        if (InsPlan_1_Material_Code == "") InsPlan_1_Material_Code = ins_plan_1.Material_Code;

                        if (string.IsNullOrEmpty(ins_plan_1.Material_Code) && TaskAPI.Process != "MTO Base Code Application")
                        {
                            regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;S" + (i + 1).ToString() + "-1 => 行" + ins_plan_1.No + "中 SAP Code 为必填"));
                        }

                        if (string.IsNullOrEmpty(ins_plan_1.Plant))
                        {
                            regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;S" + (i + 1).ToString() + "-1 => 行" + ins_plan_1.No + "中 Plant 为必填"));
                        }
                    }
                }

                //if ((TaskAPI.Process == "New Raw Material Introducing" || TaskAPI.Process == "Raw Code Pack Size Change" || TaskAPI.Process == "Raw Code Extend Application") && (Page as PMDC).TabMaterial1.Technical1.TEC_Inspection_Setup.SelectedValue == "Y" && string.IsNullOrEmpty((InsPlan_1[0] as List<mdc_Ins_Plan_1>)[0].Material_Code))
                //foreach (mdc_Ins_Plan_1 ins_plan_1 in ins_plan_s)
                //{
                //    if ((TaskAPI.Process == "New Raw Material Introducing" || TaskAPI.Process == "Raw Code Pack Size Change" || TaskAPI.Process == "Raw Code Extend Application") && (Page as PMDC).TabMaterial1.Technical1.TEC_Inspection_Setup.SelectedValue == "Y" && string.IsNullOrEmpty(ins_plan_1.Plant))
                //    {
                //        regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;S1-1=> Inspection Plan 1 为必填"));
                //    }
                //}
        
            //if ((TaskAPI.Process == "Sefi BomIP Application" && (Page as PMDC).INP.Checked && string.IsNullOrEmpty((InsPlan_1[0] as List<mdc_Ins_Plan_1>)[0].Material_Code)))
            //{
            //    regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;S1-1=> Inspection Plan 1 为必填"));
            //}
            #endregion

            #region InsPlan_2
            for (int i = 0; i < InsPlan_2.Count; i++)
            {
                List<mdc_Ins_Plan_2> ins_plan_s = InsPlan_2[i] as List<mdc_Ins_Plan_2>;
                if (ins_plan_s == null)
                {
                    continue;
                }

                foreach (mdc_Ins_Plan_2 ins_plan_2 in ins_plan_s)
                {
                    if (string.IsNullOrEmpty(ins_plan_2.MstrChar_Code))
                    {
                        continue;
                    }

                    if (!string.IsNullOrEmpty(ins_plan_2.MstrChar_Code) && string.IsNullOrEmpty(ins_plan_2.QN_QL))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;S" + (i + 1).ToString() + "-2 => 行" + ins_plan_2.No + "中 Mstr Char Code 填写错误"));
                        continue;
                    }

                    if (ins_plan_2.QN_QL.Contains("定性"))
                    {
                        continue;
                    }

                    double d1 = 0.0;
                    double d2 = 0.0;
                    double.TryParse(ins_plan_2.Lower_SpecLimit, out d1);
                    double.TryParse(ins_plan_2.Upper_SpecLimit, out d2);
                    if (d1 > d2)
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;S" + (i + 1).ToString() + "-2 => 行" + ins_plan_2.No + "中 Upper SpecLimit 应大于 Lower SpecLimit "));
                    }

                    int dn = 0;
                    int.TryParse(ins_plan_2.Decimal_Number.Trim(), out dn);
                    if (dn >= 0)
                    {
                        regs.Add(new Regular(dn == 0 ? RegularExp.Digit0 : (dn == 1 ? RegularExp.Digit1 : (dn == 2 ? RegularExp.Digit2 : (dn == 3 ? RegularExp.Digit3 : (dn == 4 ? RegularExp.Digit4 : RegularExp.Digit5)))), ins_plan_2.Lower_SpecLimit.Trim().Replace("-", ""), "&nbsp;&nbsp;S" + (i+1).ToString() + "-2 => 行" + ins_plan_2.No + "中 Lower SpecLimit 小数点应该在 Decimal number规定范围内"));
                        regs.Add(new Regular(dn == 0 ? RegularExp.Digit0 : (dn == 1 ? RegularExp.Digit1 : (dn == 2 ? RegularExp.Digit2 : (dn == 3 ? RegularExp.Digit3 : (dn == 4 ? RegularExp.Digit4 : RegularExp.Digit5)))), ins_plan_2.Upper_SpecLimit.Trim().Replace("-", ""), "&nbsp;&nbsp;S" + (i + 1).ToString() + "-2 => 行" + ins_plan_2.No + "中 Upper SpecLimit 小数点应该在 Decimal number规定范围内"));
                    }
                }

                //if ((TaskAPI.Process == "New Raw Material Introducing" || TaskAPI.Process == "Raw Code Pack Size Change" || TaskAPI.Process == "Raw Code Extend Application") && (Page as PMDC).TabMaterial1.Technical1.TEC_Inspection_Setup.SelectedValue == "Y" && !string.IsNullOrEmpty((InsPlan_1[0] as List<mdc_Ins_Plan_1>)[0].Material_Code))
                //foreach (mdc_Ins_Plan_2 ins_plan_2 in ins_plan_s)
                //{
                //    if ((TaskAPI.Process == "New Raw Material Introducing" || TaskAPI.Process == "Raw Code Pack Size Change" || TaskAPI.Process == "Raw Code Extend Application") && (Page as PMDC).TabMaterial1.Technical1.TEC_Inspection_Setup.SelectedValue == "Y" && string.IsNullOrEmpty(ins_plan_2.MstrChar_Code))
                //    {
                //        regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;S-2=> Inspection Plan 2 为必填"));
                //    }
                //}
            }
            #endregion

            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Category + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;
        }
        #endregion

        #region 方法 gv_InsPlan 数据取得
        protected void gv_InsPlan_2_DataBinding(object sender, EventArgs e)
        {
            if (gv_InsPlan_2.ChangedColumnIndex == 3)
            {
                List<mdc_Ins_Plan_2> ins_plan_s = gv_InsPlan_2.DataSource as List<mdc_Ins_Plan_2>;

                m_QS_List qs = (m_QS_List.master_insp_charac == ins_plan_s[gv_InsPlan_2.ChangedRowIndex].MstrChar_Code).ListFirst<m_QS_List>();
                if (qs != null)
                {
                    ins_plan_s[gv_InsPlan_2.ChangedRowIndex].MstrChar_Plant = qs.Plant;
                    ins_plan_s[gv_InsPlan_2.ChangedRowIndex].QN_QL = qs.Code_Group_Sel_Set;
                    ins_plan_s[gv_InsPlan_2.ChangedRowIndex].Short_Text_In_Insp_Char = qs.Description_ZH;
                    ins_plan_s[gv_InsPlan_2.ChangedRowIndex].Decimal_Number = qs.DecPl;
                    ins_plan_s[gv_InsPlan_2.ChangedRowIndex].Lower_SpecLimit = qs.Lower_SpecLimit;
                    ins_plan_s[gv_InsPlan_2.ChangedRowIndex].Upper_SpecLimit = qs.Upper_SpecLimit;
                    //ins_plan_s[gv_InsPlan_2.ChangedRowIndex].Long_Text = qs.Short_Text;
                }

                gv_InsPlan_2.DataSource = ins_plan_s;
            }

            //定性的情况，将 Lower_SpecLimit、Upper_SpecLimit 置空
            {
                List<mdc_Ins_Plan_2> ins_plan_s = gv_InsPlan_2.DataSource as List<mdc_Ins_Plan_2>;
                if (gv_InsPlan_2.ChangedRowIndex >= 0 && gv_InsPlan_2.ChangedRowIndex < ins_plan_s.Count 
                                        && !string.IsNullOrEmpty(ins_plan_s[gv_InsPlan_2.ChangedRowIndex].QN_QL) 
                                        && ins_plan_s[gv_InsPlan_2.ChangedRowIndex].QN_QL.Contains("定性"))
                {
                    ins_plan_s[gv_InsPlan_2.ChangedRowIndex].Upper_SpecLimit = "";
                    ins_plan_s[gv_InsPlan_2.ChangedRowIndex].Lower_SpecLimit = "";
                }
            }
        }
        #endregion

        #region 事件 Import_Click
        protected void Import_Click(object sender, EventArgs e)
        {
            //上传附件文件检查
            if (string.IsNullOrEmpty(Attachment1.FirstFilePath) || Attachment1.FirstFilePath.ToLower().LastIndexOf(".xls") == -1)
            {
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('模板文件未上传或导入模板文件格式不正确！',false)");
                return;
            }

            ExcelMapper em = new ExcelMapper();
            InsPlan_I ipi = new InsPlan_I();

            List<InsPlan_I> list = em.Read<InsPlan_I>(ipi, Attachment1.FirstFilePath);
            InsPlan_1.Clear();
            InsPlan_2.Clear();
            Sheets.Items.Clear();
            if (list.Count == 1)
            {
                SheetIndex = 0;
                SheetsCount.Text = Sheets.Items.Count.ToString();
            }
            for (int i = 0; i < list.Count; i++)
            {
                Sheets.Items.Add(new ListItem((i + 1).ToString(), i.ToString()));

                List<mdc_Ins_Plan_1> ip1_s = new List<mdc_Ins_Plan_1>();
                mdc_Ins_Plan_1 ip1 = new mdc_Ins_Plan_1();
                ip1.Material_Code  = list[i].SAP_Material_Code;
                ip1.Material_Description = list[i].Material_Description;
                ip1.Plant = list[i].Plant;
                ip1.IP_Group = list[i].IP_Group;
                ip1.Group_Counter = list[i].Group_Counter;
                ip1.Sheets = i;
                int status = 0;
                bool flag = false;
                if (!string.IsNullOrEmpty(list[i].Status))
                {
                    int.TryParse(list[i].Status.Substring(0, 1), out status);
                }
                ip1.Status = status;
                ip1_s.Add(ip1);

                InsPlan_1.Add(ip1_s);
                int no = 10;
                List<mdc_Ins_Plan_2> ip2_s = new List<mdc_Ins_Plan_2>();
                for (int j = 0; j < list[i].A1.Count; j++)
                {
                    if (string.IsNullOrEmpty(list[i].A2[j].ToString()))
                    {
                        continue;
                    }

                    mdc_Ins_Plan_2 ip2 = new mdc_Ins_Plan_2();
                    //int.TryParse(list[i].A1[j].ToString(), out no);
                    ip2.No = no;
                    ip2.QN_QL = list[i].A2[j].ToString();
                    ip2.MstrChar_Plant = list[i].A3[j].ToString();
                    ip2.MstrChar_Code = list[i].A4[j].ToString();
                    ip2.Short_Text_In_Insp_Char = list[i].A5[j].ToString();
                    ip2.Lower_SpecLimit = list[i].A6[j].ToString();
                    ip2.Upper_SpecLimit = list[i].A7[j].ToString();
                    ip2.Decimal_Number = list[i].A8[j].ToString();
                    ip2.Long_Text = list[i].A9[j].ToString();
                    ip2_s.Add(ip2);
                    ip2.Sheets = i;
                    no = no + 10;
                }

                InsPlan_2.Add(ip2_s);
            }

            gv_InsPlan_1.DataSource = InsPlan_1[0];
            gv_InsPlan_2.DataSource = InsPlan_2[0];

            SheetsCount.Text = Sheets.Items.Count.ToString();

            Anthem.Manager.AddScriptForClientSideEval("CellClick('" + gv_InsPlan_1.ClientID + "','0','0');");
        }
        #endregion

        #region 事件 Export_Click
        protected void Export_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelMapper em = new ExcelMapper();
                List<InsPlan_I> list = new List<InsPlan_I>();

                for (int i = 0; i < InsPlan_1.Count; i++)
                {
                    InsPlan_I ipi = new InsPlan_I();

                    List<mdc_Ins_Plan_1> ip1_s = InsPlan_1[i] as List<mdc_Ins_Plan_1>;

                    ipi.SAP_Material_Code = ip1_s[0].Material_Code;
                    ipi.Material_Description = ip1_s[0].Material_Description;
                    ipi.Plant = ip1_s[0].Plant;
                    ipi.IP_Group = ip1_s[0].IP_Group;
                    ipi.Group_Counter = ip1_s[0].Group_Counter;
                    ipi.Status = ip1_s[0].Status.ToString();

                    List<mdc_Ins_Plan_2> ip2_s = InsPlan_2[i] as List<mdc_Ins_Plan_2>;
                    for (int j = 0; j < ip2_s.Count; j++)
                    {
                        ipi.A1.Add(ip2_s[j].No);
                        ipi.A2.Add(ip2_s[j].QN_QL);
                        ipi.A3.Add(ip2_s[j].MstrChar_Plant);
                        ipi.A4.Add(ip2_s[j].MstrChar_Code);
                        ipi.A5.Add(ip2_s[j].Short_Text_In_Insp_Char);
                        ipi.A6.Add(ip2_s[j].Lower_SpecLimit);
                        ipi.A7.Add(ip2_s[j].Upper_SpecLimit);
                        ipi.A8.Add(ip2_s[j].Decimal_Number);
                        ipi.A9.Add(ip2_s[j].Long_Text);
                    }

                    list.Add(ipi);
                }

                string path = Path.Combine(Util.AppSettings("path"), TaskAPI.Process);
                string date = System.DateTime.Now.ToString("yyyyMMddHHmmssfff");

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                em.Write<InsPlan_I>(list, path + @"\" + date + @"~Inspection Plan.xls", Request.PhysicalApplicationPath + "Resource\\Xls\\" + @"Inspection Plan Template.xls");

                string FileName = Util.Encrypt(TaskAPI.Process + @"\" + date + @"~Inspection Plan.xls");

                //下载
                Anthem.Manager.AddScriptForClientSideEval("FileHandler('" + ResolveUrl("~/FileHandler.ashx") + "?File=" + FileName + "');");
            }
            catch (Exception ex)
            {
                TaskDB.Log("", false, "Export_Click Error:" + ex.ToString());
            }
        }
        #endregion

        #region 辅助类 InsPlan_I
        public class InsPlan_I
        {

            public string _SAP_Material_Code = "";
            public string _Plant = "";
            public string _Material_Description = "";
            public string _IP_Group = "";
            public string _Group_Counter = "";
            public string _Status = "";

            public ArrayList _A1 = new ArrayList();
            public ArrayList _A2= new ArrayList();
            public ArrayList _A3 = new ArrayList();
            public ArrayList _A4 = new ArrayList();
            public ArrayList _A5 = new ArrayList();
            public ArrayList _A6 = new ArrayList();
            public ArrayList _A7 = new ArrayList();
            public ArrayList _A8 = new ArrayList();
            public ArrayList _A9 = new ArrayList();

            [FromCell("A10")]
            [ToCell("A10")]
            public string SAP_Material_Code
            {
                get { return _SAP_Material_Code; }
                set { _SAP_Material_Code = value; }
            }

            [FromCell("C10")]
            [ToCell("C10")]
            public string Plant
            {
                get { return _Plant; }
                set { _Plant = value; }
            }

            [FromCell("D10")]
            [ToCell("D10")]
            public string Material_Description
            {
                get { return _Material_Description; }
                set { _Material_Description = value; }
            }

            [FromCell("G10")]
            [ToCell("G10")]
            public string IP_Group
            {
                get { return _IP_Group; }
                set { _IP_Group = value; }
            }

            [FromCell("H10")]
            [ToCell("H10")]
            public string Group_Counter
            {
                get { return _Group_Counter; }
                set { _Group_Counter = value; }
            }

            [FromCell("I10")]
            [ToCell("I10")]
            public string Status
            {
                get { return _Status; }
                set { _Status = value; }
            }

            [FromRange("A14", "A53")]
            [ToRange("A14", "A53")]
            public ArrayList A1
            {
                get { return _A1; }
                set { _A1 = value; }
            }

            [FromRange("B14", "B53")]
            [ToRange("B14", "B53")]
            public ArrayList A2
            {
                get { return _A2; }
                set { _A2 = value; }
            }

            [FromRange("C14", "C53")]
            [ToRange("C14", "C53")]
            public ArrayList A3
            {
                get { return _A3; }
                set { _A3 = value; }
            }

            [FromRange("D14", "D53")]
            [ToRange("D14", "D53")]
            public ArrayList A4
            {
                get { return _A4; }
                set { _A4 = value; }
            }

            [FromRange("E14", "E53")]
            [ToRange("E14", "E53")]
            public ArrayList A5
            {
                get { return _A5; }
                set { _A5 = value; }
            }

            [FromRange("F14", "F53")]
            [ToRange("F14", "F53")]
            public ArrayList A6
            {
                get { return _A6; }
                set { _A6 = value; }
            }

            [FromRange("G14", "G53")]
            [ToRange("G14", "G53")]
            public ArrayList A7
            {
                get { return _A7; }
                set { _A7 = value; }
            }

            [FromRange("H14", "H53")]
            [ToRange("H14", "H53")]
            public ArrayList A8
            {
                get { return _A8; }
                set { _A8 = value; }
            }

            [FromRange("I14", "I53")]
            [ToRange("I14", "I53")]
            public ArrayList A9
            {
                get { return _A9; }
                set { _A9 = value; }
            }
        }
        #endregion
    }
}
